

function ALOGsaveHack() {
    function prepareHack(hack) {
        function prepareSector(pos, sector) {
        var i = sector.indexOf("[");
        if (i != -1) {
            pos.sector = sector.substr(0, i - 1);
            pos.coords = sector.substr(i);
        } else
            pos.sector = sector;
        }
    
        function getPosition(posCell) {
            var len = posCell.childNodes.length;
            var result = {sector: null, coords: null};
            result.cluster = posCell.childNodes[len - 1].textContent;
            if (len > 1)
                prepareSector(result, posCell.childNodes[0].nodeValue);
            return result;
        }
    
        function prepareResources(resourceCell) {
            var result = [];
            var res = null;
            for (var i = 0; i < resourceCell.childNodes.length; i++) {
                var node = resourceCell.childNodes[i];
                var tag = node.tagName;
                if (tag) {
                    if (tag.toLowerCase() == "br")
                        continue;
                else
                    if (tag.toLowerCase() == "img") {
                        var name = node.src;
                        name = name.substr(name.lastIndexOf("/") + 1);
                        res = {img: name};
                    }
                }
                else {
                    var amount = node.nodeValue;
                    res.amount = amount.substring(amount.indexOf(" ") + 1, amount.length - 1);
                    result.push(res);
                }
            }
            return result;
        }
    
        var str = document.location.hostname;
        var universe = str.substring(0, str.indexOf("."));
        if (!universe)
            universe = "orion";
        hack.universe = universe.charAt(0).toUpperCase() + universe.substring(1);
        
        var table = document.getElementsByTagName("table")[0];
        table = table.rows[1].cells[1].getElementsByTagName("table")[0];
        var cell = table.rows[0].cells[0];
        var tables = [];
        for (var i = 0; i < cell.childNodes.length; i++) {
            var child = cell.childNodes[i];
            var tag = child.tagName;
            if (tag && (tag.toLowerCase() == "table"))
                tables.push(child);
        }
        hack.location = table.getElementsByTagName("a")[0].innerHTML;
    
        var buildingTable = null;
        var foeTable = null;
        var friendTable = null;
        var resourceTable = null;
        var shipTable = null;
    
        switch (tables.length) {
            case 2:
                hack.method = "brute";
                break;
            case 4:
                hack.method = "skilled";
                buildingTable = tables[2];
                foeTable = tables[3];
                break;
            case 5:
                hack.method = "freak";
                buildingTable = tables[2];
                foeTable = tables[3].getElementsByTagName("table")[0];
                friendTable = tables[3].getElementsByTagName("table")[1];
                resourceTable = tables[4];
                break;
            case 6:
                hack.method = "guru";
                shipTable = tables[2];
                buildingTable = tables[3];
                foeTable = tables[4].getElementsByTagName("table")[0];
                friendTable = tables[4].getElementsByTagName("table")[1];
                resourceTable = tables[5];
                break;
            default:
                return false;
        }
    
        var pilotTable = tables[1];
        hack.date = new Date(pilotTable.rows[0].cells[0].innerHTML.replace(/-/g, "/")).getTime();
        hack.pilot = pilotTable.rows[1].cells[0].textContent;
        hack.credits = pilotTable.rows[3].cells[0].innerHTML.replace(/,/g, "");
        hack.reputation = pilotTable.rows[3].cells[1].innerHTML.replace(/,/g, "");
        hack.building_amount = pilotTable.rows[3].cells[2].innerHTML;
        if (pilotTable.rows.length >= 6)
            hack.experience = pilotTable.rows[5].cells[0].innerHTML.replace(/,/g, "");
        if (pilotTable.rows.length >= 8)
            hack.position = getPosition(pilotTable.rows[7].cells[0]);
    
        var lookupCell = tables[0].rows[1].cells[0];
        var pilotSelect = lookupCell.getElementsByTagName("select")[0];
        if (pilotSelect) {
            for (var i = 0; i < pilotSelect.length; i++) {
                var option = pilotSelect.item(i);
                if (option.text == hack.pilot) {
                    hack.pilot_id = option.value;
                break;
            }
        }
        } else {
            var pilotInput = lookupCell.getElementsByTagName("input")[1];
            hack.pilot_id = pilotInput.value;
        }
        
        if (shipTable != null) {
            hack.ship_status = {};
            for (var i = 1; i < shipTable.rows.length; i++) {
                var font = shipTable.rows[i].cells[0].getElementsByTagName("font")[0];
                var name = font.innerHTML;
                name = name.substr(0, name.indexOf(":")).toLowerCase();
                var status = {};
                status.color = font.color;
                status.amount = shipTable.rows[i].cells[1].getElementsByTagName("table")[0].width;
                hack.ship_status[name] = status;
            }
        }
    
        hack.building_positions = [];
        if (buildingTable != null) {
            for (var i = 1; i < buildingTable.rows.length; i++) {
                var pos = getPosition(buildingTable.rows[i].cells[1]);
                pos.amount = buildingTable.rows[i].cells[0].innerHTML;
                hack.building_positions.push(pos);
            }
        }
    
        hack.foes = [];
        hack.foe_alliances = [];
        var alliance = false;
        if (foeTable != null) {
            for (var i = 1; i < foeTable.rows.length; i++) {
                var cell = foeTable.rows[i].cells[0];
                if (cell.tagName.toLowerCase() == "th") {
                    alliance = true;
                    continue;
                }
                if (alliance)
                    hack.foe_alliances.push(cell.textContent);
                else
                    hack.foes.push(cell.textContent);
            }
        }
    
        hack.friends = [];
        hack.friend_alliances = [];
        alliance = false;
        if (friendTable != null) {
            for (var i = 1; i < friendTable.rows.length; i++) {
                var cell = friendTable.rows[i].cells[0];
                if (cell.tagName.toLowerCase() == "th") {
                    alliance = true;
                    continue;
                }
                if (alliance)
                    hack.friend_alliances.push(cell.textContent);
                else
                    hack.friends.push(cell.textContent);
            }
        }
    
        if (resourceTable) {
            hack.buildings = [];
            var buildingTables = resourceTable.getElementsByTagName("table");
            for (var i = 0; i < buildingTables.length; i++) {
                var posStr = buildingTables[i].rows[0].cells[0].innerHTML;
                posStr = posStr.substr(new String("Building in ").length);
                var building = {};
                prepareSector(building, posStr);
                for (var j = 0; j < hack.building_positions.length; j++)
                    if (building.sector == hack.building_positions[j].sector) {
                        building.cluster = hack.building_positions[j].cluster;
                        break;
                    }
                building.commodities = prepareResources(buildingTables[i].rows[2].cells[0]);
                building.stock = prepareResources(buildingTables[i].rows[2].cells[1]);
                hack.buildings.push(building);
            }
        }
    
        return true;
    }

    function objectToXml(obj, nodeName, parentNode) {
        if (!parentNode)
            parentNode = document.implementation.createDocument("", "", null);
        var node = document.createElement(nodeName);
        parentNode.appendChild(node);
        if (obj instanceof Array) {
            var collection = node.ownerDocument.createAttribute("collection");
            collection.value = true;
            node.attributes.setNamedItem(collection);
            var name = nodeName.substr(0, nodeName.length - 1);
            for (var i = 0; i < obj.length; i++)
                objectToXml(obj[i], name, node);
        } else
            if (typeof(obj) == "object") {
                var i;
                for (i in obj)
                objectToXml(obj[i], i, node);
            } else
                node.appendChild(node.ownerDocument.createTextNode(obj));
        return parentNode;
    }

    function xmlToStr(doc) {
        return new XMLSerializer().serializeToString(doc);
    }

    function postHack(data) {
        var username = "C0mbatL0gger"; 
        var password = "vAd9Z7BEt6";
        
        var url = POgetServerUrl("hack_add.php", false);
        var query =
            "acc="+username
            + "&pwd="+password
            + "&data="+data
            + "&userid="+POgetUserID ()
            + "&uname="+POgetUserName ()
            + "&vcode="+POgetValidationCode ();
        POajaxPost (url, "POST", query, null);
        document.getElementById ("po_alog_confirm").textContent = "Hack submitted to Federation database. Thank you!";
    }

    var hack = {};
    if (!prepareHack(hack))
        return;
    var doc = objectToXml(hack, "hack");
    postHack(xmlToStr(doc));
}

function ALOGchangeCheckbox() {
    localStorage.setItem ("po_alog_submit_logs", String(document.getElementById ('alog_checkbox').checked));
}

function ALOGaddShareBtn() {
    var type = document.hacking.method;
    var method = type.options[type.selectedIndex].textContent;
    if (method == "Hack Trade Log" || method == "Hack Object Log") return;
    
    var tbl = document.getElementById ("doHackButton").parentNode.parentNode.parentNode;
    var parent = tbl.parentNode.parentNode;
    localStorage.setItem ("po_alog_submit_logs", "true");
    var div = document.createElement ("tr");
    div.innerHTML = "<td colspan=2 align=center><div style='color:lime;font-size:14px'><input id='alog_checkbox' type='checkbox' checked='true' onclick='ALOGchangeCheckbox();'> submit the hack log automatically to the Federation database</input>\
                    </div><div style='color:lime;font-size:14px' id='po_alog_confirm'></div></td>";
    tbl.appendChild (div);
    
    if (parent.getElementsByTagName ("table").length > 1) {
        // The hack has been performed
        if (localStorage.getItem ("po_alog_submit_logs") == "true") {
            ALOGsaveHack ();
        }
    }
}

ALOGaddShareBtn();